#ifndef DAN_UIFX
#define DAN_UIFX
#include "UVOperator.cginc"


//---------------------------闪烁 干扰 -----------------------------
//间歇闪烁
float Blink01(float speed, float steps){
	float offsetTime =fmod(_Time.y*speed,steps);
	float flowX = floor(offsetTime)/steps;
	return flowX;
}
//快速闪动
float Blink02 (float speed , float intensity){
speed*=_Time.y;
return cos(speed+cos(speed*90)*100)*intensity+0.5;
}

//模拟noise闪烁
float Flick (float2 timeSpeed){
	float2 sk = timeSpeed + 0.2127+timeSpeed.x*0.3713*timeSpeed.y;
	float2 rnd = 4.789*sin(489.123*(sk));
	return frac(rnd.x*rnd.y*(1+sk.x));
}
//缓慢闪动 返回值为float2
float2 FlickSin(float speed , float amplitude){
	float flicker = (sin(_Time.y*speed)+1)*amplitude;
	float2 sin = float2(-flicker,flicker);
	return sin;
}
//随机闪动 gradient 渐变或形状
float FlickerRD (float gradient , float speed, float amplitude){
	float2 rd = float2(gradient,gradient)+_Time.y*0.001*speed;
	float rdSpeed = Flick(rd);
	float flick = saturate( ( rdSpeed  * 0.5 ) / amplitude +0.5);
	return flick;
}




//抖动 闪烁 flick
void ShakeShinnyUV01 (float2 uv,float shakeStregth, float shinnyAmount,out float2 shakeUV,out float flick ,out float shinny){     
	float2 flickTime = float2(_Time.x,_Time.x)*0.01;
	flick = Flick(flickTime);
	float shakeST = flick*0.05*shakeStregth;
	shakeUV = shakeST-shakeST*0.5+uv;
	shinny = saturate(flick+(1.0 - shinnyAmount));

}

//震动 明暗闪烁
void ShakeShinnyUV02 (float2 uv ,float shakeSpeed,float shakeStrength, float shinnyAmount,out float2 noiseUV,out float shinny){
    float s1 = _Time.x*shakeSpeed*0.05;//定义两种震动时间
    float s2 = _Time.x*shakeSpeed*0.08;
   
    float shakeX = Flick(float2(s1,s1));//计算两种随机震动
    float shakeY = Flick(float2(s2,s2));
    noiseUV = frac(float2(shakeX,shakeY)*0.005*shakeStrength+uv);//UV震动
    shinny = (clamp((shakeX+shakeY),0,0.5)*shinnyAmount)+1;//计算明暗
 }
//马赛克闪动
 float Mosaic01(float2 uv, float density, float speed, float amount){                

 	float2 rd= floor((uv+1) * density) / (density - 1)+speed;
 	float flick = Flick(rd);
 	float bg = saturate((1.1 - amount)*3.333333-2.333333);
 	float mosaicRD =saturate(smoothstep( amount, 1.0, flick )+bg*bg);
 	return mosaicRD;
 }    
//扰动线条带闪烁
 float ScanLine01 (float uvdir, float shinnySpeed, float strength, float lineContrast , float lineDensity, float lineSpeed){
 	float stime = _Time.y*0.001*shinnySpeed;                
 	float shinny = Flick(float2(stime,stime));
 	float scaneLine =saturate(lineContrast+abs(sin(uvdir*lineDensity+_Time.y*lineSpeed)));
 	float shinnyStrength =saturate(strength +  shinny *(1 - strength));
 	return scaneLine*shinnyStrength;
 }
 //扰动线条简单
 float ScanLine02 (float uvdir,float lineDensity,float lineSpeed, float lineContrast){
 	float lineUVDir = uvdir*lineDensity;
 	lineSpeed = _Time.y*lineSpeed;
 	float scanLine =step(0.1,frac(lineUVDir+lineSpeed)*2-1)+lineContrast;
 	return scanLine;
 }



//马赛克扰动  在表面扰动 带淡入淡出
 float Mosaic02(float2 uv , float density , float mosaicspeed , float mosaicU, float mosaicV, float fade){
 	float2 ruv = abs(uv*2.0-1.0);
 	float layerUV = floor((1.0 - saturate(saturate(max(ruv.x,ruv.y)))) * density) / (density - 1);
 	float speed = _Time.y*0.001*mosaicspeed;
 	float2 rd = floor((uv*float2(mosaicU,mosaicV)) * density) / (density - 1)+speed;
 	float flick = Flick(rd);
 	float mosaic= step(fade,(layerUV+layerUV*flick)/2.0)*flick;
 	return smoothstep( 0, fade, mosaic );
 }


//------------------------ 速度 动效 --------------------
//sintime
 float UseSinTime (float percent, float speed , float useTimeToggle, float useSinToggle )
 {
    float useTime = lerp( percent, frac(speed), useTimeToggle );//根据time百分比
    float useSintime =lerp( useTime, saturate(sin(speed)), useSinToggle )*1.1-0.55;//SinTime
    return useSintime ;
 }
//looptime
 float UseTimeLoop(float loopSpeed , float percent, float usetimeToggle){
 	float loopTime = saturate((abs(frac(loopSpeed)-0.5)*2.0)*1.5-0.5);
 	float usetime = lerp( percent, loopTime, usetimeToggle );
 	return usetime;
 }

//------------------------- UI 动效 ----------------------

//展开效果 制作图片的打开动效
 float4 OpenCanvas (float2 uv, sampler2D tex, float open){
 	open*=0.5; 
 	float4 moveRight = tex2D(tex,float2(uv.x-open,uv.y));
 	float roundU = round(uv.x);                          
 	float4 moveLeft = tex2D(tex,float2((1 - uv.x)-open,uv.y));                
 	float4 RGBChannel = moveRight*(1 - roundU)+moveLeft*roundU;
 	return RGBChannel;
 }
//启动故障 用于固定方向关闭
 void InitiateGlitch (float open, float flick ,out float initiate){
 	initiate = step(0.95,((1- open)+flick));

 }
//衰减故障 用于缩放消失
void FadeoutGlitch (float scale, float flick ,out float fadeOut){
	fadeOut = saturate((step(0.5,flick)+(scale*2-1)));
}
//根据形状分层
float ShapeSteps (float shape , float steps , float  seed  ){
	float circleLayer = floor(shape * steps) / (steps - 1);
	float2 rd = float2(circleLayer,circleLayer)+float2(seed,seed);  
	return Flick(rd);
}


//根据形状分割扩散
 float ShapeFlow00 (float shape ,float amount,float speed,float width,float blur){  
 	shape = frac(shape*amount+speed);
 	return shape =smoothstep(width,width+blur,shape);
 }  
//环绕流动
 float AroundFlow(float shape , float polar, float speed , float2 widthSize){
 	float polarFlow = frac((polar+(_Time.y*speed)));
 	float mask = saturate(abs(step(widthSize.x,shape)-step(widthSize.y,shape)));
 	return polarFlow*mask;
 }



//根据形状分割扩散 可选两种动画类型
 float ShapeFlow01(float shape , float panSpeed, float usePan, float pingpongSpeed, float usePingpong, float glowup, float level,float gap )
 {

 	float panTime =(1 - shape)+(_Time.y*panSpeed);
 	float useTimeForPan = lerp( shape, panTime, usePan );
 	float shapeFlow = useTimeForPan * level;
 	float gapLevel = step(gap,1 - frac(shapeFlow));

 	float shinnTime =(frac(_Time.y*pingpongSpeed)-0.5)*2;
 	shinnTime =saturate(abs(shinnTime)*1.25-0.125);
 	float usePin = lerp( glowup, shinnTime , usePingpong );

 	float useStyle = step((usePin*1.01),floor(shapeFlow) / (level - 1));

 	return useStyle*gapLevel;
 }

//中空,外圈分割扩散  可返回 扩散动效 中央半径 分割后的形状
void ShapeFlow02(float shape , float radius , float gapWidth, float level ,
                float usetime, out float flowPoly , out float centerRad, out float gapPoly){
    //中央半径
      centerRad = saturate((1 - shape - radius ) / (1 - radius));
    //分割多边形
      gapPoly = step(gapWidth,frac(centerRad*level));
    //图形收缩动效               
      flowPoly =step(usetime*1.01,floor(centerRad * level) / (level - 1))*gapPoly;
}

//柱状生成 
//barDir为柱状uv的浮动方向 barStyle为排列方式=极坐标 uv方向等等
void BarGraph (float barDir, float barStyle , float density, float speed ,
	float lengthCon,float length , float width, float mask )
{
	float gradient = floor(barStyle * density) / (density - 1);//根据形状出渐变梯度
	//随机变化
	float2 rd = (float2(gradient,gradient)+(_Time.y*0.001*speed));
	float flick = Flick(rd);
	//闪烁
	float flicker =barDir+ flick*lengthCon;
	//柱状
	float bar = step(flicker,length);
	//遮罩
	float maskWidth =step(frac(barStyle*density),width);
	float maskHeight = step(mask,barDir);
	//裁切
	clip(bar*maskWidth*maskHeight - 0.5);
}

// 进度条
// uv方向，分层，动效，分割，宽度，宽度UV方向 
void LoadingLine (float loadingDir, float levelamount, float useSintime,
	float gapVal, float widthVal,float widthDir)
{
    float level = floor(abs(levelamount));//分割数量
    float dir = floor((1.0 - loadingDir) * level) / (level - 1);//UV方向渐变黑白

    float gap = saturate(1- step(gapVal,frac(loadingDir*level)));//分割形状
    float width =saturate(1-step( widthVal, abs(widthDir*2-1)));//宽度
    float loadingline = step(0.5,useSintime+dir);//求增长条
    clip(loadingline*gap*width - 0.5);//剔除
}

//极坐标间隔旋转效果 走马灯
float PolarGap (float polar,float speed ,float usePer, float loadingPer , float width , float level){

 float usePercent = lerp( frac(_Time.y*speed), loadingPer, usePer ); 
 float polarfrac = floor(polar * level) / (level - 1);
 float oneminusPolar = 1.0 - polarfrac;
 float s1 =step(0.5,(-usePercent*2.2+1.65)+oneminusPolar);
 float s2 =step(0.5,(usePercent*2.2-0.55)+polarfrac);
 float gap =1.0 - step(width,frac(polar*level));
 return s1*s2*gap;
}

//根据pitch roll 方向旋转uv
void PitchRollUV( float2 uv,  float pitchVal,  float rollVal,float offset,float speed,
				  float2 scaleUV,float2 pitchUV,out float2 uvPitchRoll,out float2 rotateUV)
{

    float rad = _PI2*pitchVal;//圆周旋转
    pitchUV = Rotate2D(scaleUV,0.5,rad);//pitch uv绕x轴旋转 
    rollVal = max(rollVal,1);
    float rollY =(pitchUV.y*rollVal-(rollVal*0.5-0.5))+offset;//y轴的roll值和offset值
    uvPitchRoll =float2(pitchUV.x,rollY); //用于roll的 uv
    rotateUV = Rotate2D(uvPitchRoll,0.5,speed* _Time.y); //uv绕y轴旋转
}

//采样图片标记 放置于UV的四个角落 
void TargetCorner(float speed,float amplitude,float2 uv , sampler2D targetTex,out float targetShape ){
	float flicker = (sin(_Time.y*speed)+1)*amplitude;
	float2 motionXY = float2(-flicker,flicker);
	float2 pos = float2(0,-3);
	float2 uv_TopLeft = uv*4+pos+motionXY;
	float targetTopLeft = tex2D(targetTex,uv_TopLeft).a;
	float2 uv_BottomRight =((1 - uv)*4)+pos+motionXY;
	float targetBottomRight = tex2D(targetTex,uv_BottomRight).a;                
	float2 uv_TopRight = (float2((1 - uv.x),uv.y)*4)+pos+motionXY;
	float targetTopRight = tex2D(targetTex,uv_TopRight).a;                               
	float2 uv_BottomLeft = (float2(uv.x,(1 - uv.y))*4)+pos+motionXY;
	float targetBottomLeft = tex2D(targetTex,uv_BottomLeft).a;
	targetShape = (targetTopLeft+targetBottomRight+targetTopRight+targetBottomLeft);
	
}

//采样图片标记，放置于四边中心
void TargetCross(float speed,float amplitude,float2 uv,sampler2D targetTex, out float targetShape){
	float flicker = (sin((_Time.y*speed))+1)*amplitude;               
	float2 delta = float2(0,flicker);
	float2 pos = float2(-1,-2);
	float2 uv_Top = uv*3+pos+delta;                
	float targetTop = tex2D(targetTex,uv_Top).a;
	float2 uv_Right = float2(1 - uv.y,uv.x)*3+pos+delta;
	float targetRight = tex2D(targetTex,uv_Right).a;
	float2 uv_Bottom =(float2(1 - uv.x,1 - uv.y)*3)+pos+delta;
	float targetBottom = tex2D(targetTex,uv_Bottom).a;
	float2 uv_Left = (float2(uv.y,1 - uv.x)*3)+pos+delta;
	float targetLeft = tex2D(targetTex,uv_Left).a;
	targetShape=targetTop+targetRight+targetBottom+targetLeft;
}
//球形转动 需要一个UVmotion贴图和 一个主贴图（A通道内存放图形）
float SphereRotate(float2 uv, float scale , float glow, float rotationspeed, float backOpacity,sampler2D sphereUVTex,sampler2D mainTex)
{
	float2 sphereUV = (scale+2*uv-1)/(2*scale);
	float sphereShape = length(sphereUV*2-1);

	float fresnel = saturate(asin((1 - floor(sphereShape))*sphereShape-0.5))*glow;
    float2 speed = _Time.y*rotationspeed*float2(1,0);//给u方向速度
    float4 sphereTex = tex2D(sphereUVTex,sphereUV);//采样球贴           
    float2 motionFront = frac((sphereTex.rg+speed));//球贴的RG通道模拟前半球UV移动
    float4 frontTex = tex2D(mainTex,motionFront);//采样出主贴图的移动
    float backU = (1 - sphereTex.r)+0.5;//后半球U方向反向，偏移0.5
    float2 motionBack = frac((float2(backU,sphereTex.g)+speed));//模拟后半球的UV移动
    float4 backTex = tex2D(mainTex,motionBack);//采样出主贴图的移动
    float sphereRotation = fresnel+(frontTex.a+backTex.a*backOpacity)*sphereTex.b;//b通道内为MASK
    return  sphereRotation;
} 

//圆环边缘模糊扩散
float RingLikeFlow (float2 uv , float maskSize,float amount, float speed, float width,float blur){
  float circle =length(uv-0.5)*2;  
  float mask =1-smoothstep(0.1,maskSize,circle);
  circle =frac(circle*amount-speed);
  circle =1-smoothstep(width,width+blur,circle);
  return circle*mask;
}












#endif 